﻿using AnlianCommon.Common.Domains.PromptReport;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;

namespace SheepCommon.API.SheepCommands.FirstPregnancyTestPromptes
{
    public class FirstPregnancyTestPromptCreateHandler : IRequestHandler<FirstPregnancyTestPromptCreateCommand, Result>
    {
        private readonly IBaseRepository<FirstPregnancyTestPrompt> _firstPregnancyTestPromptRepository;
        private readonly IMapper _mapper;

        public FirstPregnancyTestPromptCreateHandler(IBaseRepository<FirstPregnancyTestPrompt> firstPregnancyTestPromptRepository, IMapper mapper)
        {
            _firstPregnancyTestPromptRepository = firstPregnancyTestPromptRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 新增首次妊检提示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(FirstPregnancyTestPromptCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                FirstPregnancyTestPrompt firstPregnancyTestPrompt = _mapper.Map<FirstPregnancyTestPrompt>(request);
                var res = await _firstPregnancyTestPromptRepository.Add(firstPregnancyTestPrompt);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Code = (int)CommonErrorCode.Added;
                    result.Msg = "添加失败";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
